const { DataTypes } = require('sequelize');
const sequelize = require('../connect'); // Adjust the path as necessary
const Station = require('./station')
const Delivery = require('./delivery')
const Package = sequelize.define('Package', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    // 前三位手机号
    phone_number_front: {
        type: DataTypes.STRING,
        allowNull: false
    },
    // 收货人姓名
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    // 后四位手机号
    phone_number_after: {
        type: DataTypes.STRING,
        allowNull: false
    },
    user_id: {
        type: DataTypes.INTEGER
    },
    station_id: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    delivery_id: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    status: {
        type: DataTypes.STRING,
        allowNull: false
    },
    recordedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW
    }

}, {
    tableName: 'packages',
    timestamps: true
});

Package.belongsTo(Station, { foreignKey: 'station_id', as: 'stations' });
Station.hasMany(Package, { foreignKey: 'station_id', as: 'packages' });
console.log("==> The table for the Package model was just (re)created!");

Package.belongsTo(Delivery, { foreignKey: 'delivery_id', as: 'deliverys' });
Delivery.hasMany(Package, { foreignKey: 'delivery_id', as: 'packages' });
console.log("==> The table for the Package model was just (re)created!");


module.exports = Package;